package main

import (
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"
	"gofile/src/config"
	"gofile/src/router"
	"gofile/src/util"
	"net/http"
	"os"
)

//var ctx = context.Background()

func action(ctx *cli.Context) error {
	// 配置Kubernetes客户端
	appCfg, err := config.InitConfig(ctx)
	if err != nil {
		logrus.Errorf("加载配置文件失败....")
		return err
	} else {
		logrus.Debugf("..%s", appCfg.File.BaseUrl)
	}
	logrus.SetLevel(logrus.DebugLevel)
	router.Router()

	logrus.Info("服务器正常启动:0.0.0.0:" + appCfg.File.ServerPort)
	err = http.ListenAndServe("0.0.0.0:"+appCfg.File.ServerPort, nil) //设置监听的端口
	if err != nil {
		//log.Fatal("ListenAndServe: ", err)
		logrus.Error(err)
	}
	//epoll
	select {}
}

func main() {

	util.LogConfig()

	app := new(cli.App)
	app.Name = "gofile"
	app.Version = "1.0"
	app.Usage = "run"
	app.Action = action
	app.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:  "cfg",
			Usage: "配置文件路径",
		},
	}
	err := app.Run(os.Args)
	if err != nil {
		logrus.Error(err)
	}

}
